package com.ebt.m.cloud;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.cloud.adapter.DownloadAdapter;
import com.ebt.m.cloud.downloader.DownloadService;
import com.ebt.m.cloud.downloader.callback.DownloadManager;
import com.ebt.m.cloud.downloader.domain.DownloadInfo;
import com.ebt.m.cloud.event.DownloadStatusChanged;
import com.ebt.m.customer.util.ToastUtil;
import com.ebt.m.customer.view.ConfirmDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Damon.Han
 */
public class DownloadingFragment extends BaseFragment {

    private RecyclerView rv;
    private View container_empty;
    private DownloadAdapter downloadAdapter;
    private DownloadManager downloadManager;
    private boolean isEdit;

    public static DownloadingFragment newInstance() {
        Bundle args = new Bundle();
        DownloadingFragment fragment = new DownloadingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void toggleEdit(boolean isEdit) {
        this.isEdit = isEdit;
        downloadAdapter.setEditMode(isEdit);
        if (!isEdit) {
            unselectAll();
        }
    }

    @Override
    protected View getLayoutView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_download, null);
    }

    @Override
    protected void initView() {
        super.initView();
        container_empty = getView().findViewById(R.id.container_empty);
        ImageView imageViewEmpty = container_empty.findViewById(R.id.img_empty);
        imageViewEmpty.setImageResource(R.drawable.ic_cloud_cache_empty);
        TextView txtEmpty = container_empty.findViewById(R.id.txt_empty);
        txtEmpty.setText("没有正在缓存的课程");
        rv = (RecyclerView) getView().findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        EventBus.getDefault().register(this);

    }

    @Override
    protected void initData() {
        super.initData();
        downloadManager = DownloadService.getDownloadManager(getActivity().getApplicationContext());
        downloadAdapter = new DownloadAdapter(getActivity());
        rv.setAdapter(downloadAdapter);
        setData();
    }

    @Subscribe
    public void onEventMainThread(DownloadStatusChanged event) {
        setData();
    }

    private void setData() {
        List<DownloadInfo> list = downloadManager.findAllDownloading();
        if (list.size() > 0) {
            container_empty.setVisibility(View.GONE);
            rv.setVisibility(View.VISIBLE);
        } else {
            container_empty.setVisibility(View.VISIBLE);
            rv.setVisibility(View.GONE);
        }
        downloadAdapter.setData(list);
        ((ActCloudCache) getActivity()).changePageTitle(1, "缓存中 " + (list.size() > 0 ? list.size() : ""));
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public DownloadAdapter getDownloadAdapter() {
        return downloadAdapter;
    }

    public void selectAll() {
        for (int i = 0; i < downloadAdapter.getData().size(); i++) {
            DownloadInfo downloadInfo = downloadAdapter.getData().get(i);
            downloadAdapter.getSelected().put(downloadInfo.getId(), true);
        }
        downloadAdapter.notifyDataSetChanged();
    }

    /**
     * 全不选
     */
    public void unselectAll() {
        downloadAdapter.getSelected().clear();
        downloadAdapter.notifyDataSetChanged();
    }

    /**
     * 删除
     */
    public void delete() {
        List<DownloadInfo> downloadInfos = new ArrayList<>();
        for (int i = 0; i < downloadAdapter.getData().size(); i++) {
            DownloadInfo downloadInfo = downloadAdapter.getData().get(i);
            if (downloadAdapter.getSelected().get(downloadInfo.getId())) {
                downloadInfos.add(downloadInfo);
            }
        }
        if (downloadInfos.size() <= 0) {
            ToastUtil.showToast("未选中");
            return;
        }
        ConfirmDialog confirmDialog = new ConfirmDialog(getContext());
        confirmDialog.setMessage("确定删除选中吗？")
                .setPositiveButton("删除", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        confirmDialog.dismiss();
                        for (int i = 0; i < downloadInfos.size(); i++) {
                            DownloadInfo downloadInfo = downloadInfos.get(i);
                            downloadManager.remove(downloadInfo);
                            try {
                                File file = new File(downloadInfo.getPath());
                                if (file.exists() && file.isFile()) {
                                    file.delete();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        setData();
                    }
                }).show();
    }
}
